
<template>
  <div class="head">
    <img src="@/assets/images/logo-head.png">
    <el-button v-if="!hasToken" type="primary" class="head_button"
      @click="redirectToblogin">登录</el-button>
    <el-dropdown v-else>
      <div class="user_name"> <i class="icon-ym icon-ym-header-userInfo"></i>
        {{ userInfo.userName }}</div>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item divided @click.native="handleLogout">
          <i class="icon-ym icon-ym-header-loginOut"></i>{{ $t('navbar.logout') }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>
<script >
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 获取 session 值
      hasToken: (localStorage.getItem('jnpf_token') == null || localStorage.getItem('jnpf_token') == '' ||
        localStorage.getItem('jnpf_token') == undefined) ? false : true,
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  methods: {
    redirectToblogin() {
      const token = localStorage.getItem('jnpf_token');
      if (token) {
        this.$router.push("/home");
      } else {
        window.open("/blogin");
      }
    },
    // 退出
    handleLogout() {
      this.$confirm('您确定要退出应用程序吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('user/logout').then(() => {
          location.reload();
        })
      }).catch(() => {
        return false
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.head {
  grid-area: head;
  width: 1200px;
  flex-direction: row;
  display: flex;
  margin: 20px auto;
  align-items: center;
  justify-content: space-between;

  img {
    height: 60px;
    width: auto;
  }

  &_button {
    height: 32px;
  }
  .user_name {
    color: #666666;
    cursor: pointer;
    &:hover {
      color: #1890ff;
    }
  }
}
</style>